Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

291
Vistas
Javascript with the "async" attribute - can it change DOM elements that are defined below it?

The body of my HTML:

<body>
   <script async src="changeParContent.js"></script>
   <script> \\some irrelevant inline code that takes a long time to run
   </script>
   <p id='par1'>Initial text</p>
</body>

And here is changeParContent.js:

document.getElementById('par1').innerHTML = "The content was changed!"

I expected the Javascript to fail, since I use the async attribute. It means that the script will be downloaded and executed while the HTML is being parsed. Now, since I intentionally slow down the HTML parsing, I expected the javascript to run before the paragraph "p1" was defined.

I thought that I did the one thing you're not supposed to do - defining the DOM elements that are used in the async script below the script.

But it somehow works and the paragraph changes. How can it be?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Async scripts don't really run in any predictable fashion, and it's mainly down to the browser's implementation. For example, you should find the the following works in Chrome:

<body>
   <script async src="./changeParContent.js"></script>
   <script>
         function sleepFor(sleepDuration){
                var now = new Date().getTime();
                while(new Date().getTime() < now + sleepDuration){ /* Do nothing */ }
        }
        sleepFor(5000);
   </script>
   <p id='par1'>Initial text</p>
</body>

However in Firefox the following error is thrown:

Uncaught TypeError: document.getElementById(...) is null

It seems that in both browsers, the slow inline script blocks both the async script from running and the rest of the DOM from loading. Then after this slow script runs, the async script runs in parallel with the rest of the DOM loading. In my experiments, it seems that in Chrome the race is won by the DOM, which causes no errors as the element is loaded by the time the async script runs. However in my Firefox test, the race is won by the async script which throws an error.

I think the lesson here is to trust best practices, even if it looks like there's no issues with your code.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda